<template>
  <div class="form-check">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>

      <el-form-item label="邮箱地址" prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="idcardNo">
        <el-input v-model="ruleForm.idcardNo"></el-input>
      </el-form-item>

      <el-form-item label="身份证" prop="identityImg" ref="image">
        <div class="up-box" style="text-align: left">
          <Upload :limit="limit" @changeImg="changeImg" />
        </div>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即创建</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import Upload from "../Upload";
import { validPhone, idCardValidity } from "@/utils/validate";

export default {
  components: {
    Upload,
  },
  data() {
    const checkIdCardValidity = function (rule, value, callback) {
      if (value) {
        if (!idCardValidity(value)) {
          callback(new Error("身份证格式错误"));
        } else {
          callback();
        }
      }
    };

     var valiIcon = (rule, value, callback) => { // 图片验证
    if (!this.hasFmt) {
      callback(new Error('请上传图片'));
    } else {
      callback();
   }
}

    return {
      limit: 2,
      hasFmt:false,
      ruleForm: {
        name: "",
        phone: "",
        email: "",
        idcardNo: "",
        identityImg: [],
      },
      rules: {
        name: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在 2 到 10 个字符",
            trigger: "blur",
          },
        ],

        phone: [{ validator: validPhone, trigger: "blur", required: true }],
        phone: [
          {
            required: true,
            message: "请输入用户手机号码",
            trigger: "change",
          },
          {
            validator: validPhone,
            trigger: "change",
          },
        ],

        idcardNo: [
          {
            required: true,
            trigger: "change",
           message:'身份证号不能为空'
          },{
            validator: checkIdCardValidity
          }
        ],
        email: [
          {
            required: true,
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: "change",
          },
        ],
        identityImg: [
          { required: true, message:'身份证照片必填', trigger: "change" }
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.ruleForm)
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 图片的上传
    changeImg(payload) {
      console.log("上传的图片");
      this.ruleForm.identityImg=payload
      if(payload.length>0){
        this.$refs.image.clearValidate()
      }
     
    },
  },
};
</script>
<style lang="less" scoped>
.form-check {
  width: 45%;
}
</style>
